//
//  MyGiftCloudViewController.m
//  eGifr
//
//  Created by Phien Tram on 8/4/13.
//  Copyright (c) 2013 eGiftr. All rights reserved.
//

#import "MyWishListViewController.h"
#import "MenuViewController.h"
#import "WishCloudViewController.h"
#import "AppDelegate.h"
#import "UserDefaultHelper.h"
#import "WishAnnotation.h"
#import "UIImageView+WebCache.h"

#define METERS_PER_MILE 1609.344

@interface MyWishListViewController () <WishCloudViewDelegate>

@property (nonatomic, strong) UIBarButtonItem *doneBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *addBarButtonItem;
@end

@implementation MyWishListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    _mapView.showsUserLocation = YES;
    [self.tableView setEditing:YES animated:YES];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.nameLabel.text = [UserDefaultHelper getUsername];
    self.myWishList = [UserDefaultHelper getMyWishList];
    [self.profileImageView setImageWithURL:[NSURL URLWithString:[UserDefaultHelper getImageProfileUrl]] placeholderImage:[UIImage imageNamed:@"icon_user_place_holder"]];
    
    //setup frame
    CGFloat tableHeight = self.myWishList.count * self.tableView.rowHeight;
    CGRect frame = self.tableView.frame;
    frame.size.height = tableHeight;
    self.tableView.frame = frame;
    [self.tableView reloadData];
    
    CGSize contentSize = self.scrollView.contentSize;
    contentSize.height = frame.origin.y + frame.size.height;
    self.scrollView.contentSize = contentSize;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([UserDefaultHelper getLatitude], [UserDefaultHelper getLongitude]);
    if (coordinate.latitude == 0 && coordinate.longitude == 0) {
        coordinate = SharedCurrentLocation.coordinate;
    }
    [_mapView removeAnnotations:_mapView.annotations];
    
    [_mapView setCenterCoordinate:coordinate];
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, 0.0, 0.0);
    viewRegion.span.longitudeDelta = 0.01f;
    viewRegion.span.latitudeDelta = 0.01f;
    [_mapView setRegion:viewRegion animated:YES];
    
    WishAnnotation *annotation = [[WishAnnotation alloc] initWithCoordinate:coordinate];
    [_mapView addAnnotation:annotation];
}

#pragma mark - IBAction methods

- (IBAction)wishCloudBarButtonItemOnClicked:(id)sender
{
    WishCloudViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WishCloud"];
    viewController.delegate = self;
    viewController.myWishList = self.myWishList;
    [self presentViewController:viewController animated:YES completion:^{
        
    }];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex
{
    return self.myWishList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"MyWishListCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.font = [UIFont boldSystemFontOfSize:16];
    cell.textLabel.text = [self.myWishList objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark - Table view delegate

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleNone;
}

- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.myWishList removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    NSString *moveObject = self.myWishList[sourceIndexPath.row];
    [self.myWishList removeObjectAtIndex:sourceIndexPath.row];
    [self.myWishList insertObject:moveObject atIndex:destinationIndexPath.row];
}

#pragma mark - WishCloudViewController delegate

- (void)wishCloudViewDone:(WishCloudViewController *)viewController
{
    [viewController dismissViewControllerAnimated:YES completion:^{
        
    }];
}


- (void)viewDidUnload {
    [self setProfileImageView:nil];
    [super viewDidUnload];
}
@end
